﻿#region
/*******************************************************************************
** File Name:		    AreaService.cs
** Creator:		        chao.jiang 
** EMail:               chao.jiang 
** Create date:	        
** Latest Modifier:
** Latest Modify date:
** Description:		    
** Modify History:
**
** Code Review:
**
** Version number:       1.0.0
*********************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.IBatisRepository;
using Phoenix.Respository;
using System.Data;
using Phoenix.Entity;
using System.Collections;

namespace Phoenix.Service
{
    public class AreaService : IAreaService
    {
		 IArea area = new AreaRepository();

         #region  Auto Generation

         public void AddArea(AreaModel model)
         {
             EArea entity = ConvertModelToEntity(model);

             area.AddArea(entity);
         }

         public void UpdateByAreaID(AreaModel model)
         {
             EArea entity = ConvertModelToEntity(model);

             area.UpdateByAreaID(entity);

         }

         public void DeleteByAreaID(System.Int32 areaID)
         {

             area.DeleteByAreaID(areaID);

         }

         public AreaModel GetByAreaID(System.Int32 areaID)
         {

             EArea entity = area.GetByAreaID(areaID);

             AreaModel model = ConvertEntityToModel(entity);


             return model;
         }


         public IList<AreaModel> GetAll()
         {
             IList<EArea> entity = area.GetAll();

             if (entity == null)
             {
                 return null;
             }

             IList<AreaModel> list = new List<AreaModel>();

             foreach (var item in entity)
             {
                 AreaModel model = ConvertEntityToModel(item);
                 list.Add(model);
             }
             return list;

         }

         private AreaModel ConvertEntityToModel(EArea entity)
         {
             if (entity == null)
             {
                 return null;
             }
             AreaModel model = new AreaModel();
             model.AreaID = entity.AreaID;
             model.AreaName = entity.AreaName;
             model.ParentID = entity.ParentID;
             model.IsEnabled = entity.IsEnabled;
             model.EntryLevel = entity.EntryLevel;
             return model;
         }

         private EArea ConvertModelToEntity(AreaModel model)
         {
             if (model == null)
             {
                 return null;
             }
             EArea entity = new EArea();
             entity.AreaID = model.AreaID;
             entity.AreaName = model.AreaName;
             entity.ParentID = model.ParentID;
             entity.IsEnabled = model.IsEnabled;
             entity.EntryLevel = model.EntryLevel;
             return entity;
         }



         #endregion

        public IList<AreaModel> GetAllProvince()
        {

            IList<EArea> entity = area.GetAllProvince();

            if (entity == null)
            {
                return null;
            }

            IList<AreaModel> list = new List<AreaModel>();

            foreach (var item in entity)
            {
                AreaModel model = ConvertEntityToModel(item);
                list.Add(model);
            }
            return list;   
        }

        public IList<AreaModel> GetByParentId(int ParentId)
        {
            IList<EArea> entity = area.GetByParentID(ParentId);

            if (entity == null)
            {
                return null;
            }

            IList<AreaModel> list = new List<AreaModel>();

            foreach (var item in entity)
            {
                AreaModel model = ConvertEntityToModel(item);
                list.Add(model);
            }
            return list;   
        }

        public string GetIDPath(int areaID)
        {
            string IDPath = "";
            Stack sk = new Stack();

            AreaModel model = GetByAreaID(areaID);

            sk.Push(model.AreaID);
            while (model.ParentID > 0)
            {
                model = GetByAreaID(model.ParentID);
                sk.Push(model.AreaID);
            }

            while (sk.Count > 0)
            {
                IDPath += sk.Pop().ToString() + "-";
            }

            IDPath = IDPath.Substring(0, IDPath.Length - 1);
            return IDPath;
        }
    }
}